package com.tbs.parkingsaleapp.UI.Adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.tbs.parkingsaleapp.Http.Actionner.ADResulter;
import com.tbs.parkingsaleapp.Http.JsonData.LocationInfo;
import com.tbs.parkingsaleapp.Http.JsonData.ParkResult;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.HomePage.HomeFragment;
import com.tbs.parkingsaleapp.UI.Listenners.BuyClickListenner;
import com.tbs.parkingsaleapp.UI.Widget.TraitShowController;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;

public class InfoWindowAdapter extends BaseAdapter {
    ArrayList<ParkResult> total;
    HomeFragment fg;
    public InfoWindowAdapter(HomeFragment fg,int info)
    {
        this.fg=fg;
        total=SystemDataMananger.FindAllParks(info);
    }

    @Override
    public int getCount() {
        return total.size();
    }

    @Override
    public Object getItem(int position) {
        return total.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View root=View.inflate(fg.getContext(), R.layout.singleinfo_fg,null);
        Banner banner=root.findViewById(R.id.infoBanner);
        ArrayList<String> imgs=new ArrayList<>();
//                ArrayList<String> titles=new ArrayList<>();
        for (String s:total.get(position).getImgPaths()
        ) {
            imgs.add(s);
            //titles.add(total.get(position).getTrait().toString());
        }
        banner.setImageLoader(new ADResulter.myImgLoader());
        banner.setImages(imgs);
//                banner.setBannerTitles(titles);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setBannerAnimation(Transformer.CubeOut);
        banner.isAutoPlay(true);
        banner.setDelayTime(10000);
        banner.start();
        TextView textView= root.findViewById(R.id.info_title);
        textView.setText(String.format("车位号：%d",total.get(position).getPark().getNumber()));
        textView=root.findViewById(R.id.info_location);
        String[] texts={"车位已售出","车位已预定","车位在售"};
        String st=texts[ total.get(position).getPark().getStatus()+1];

        ParkResult s= total.get(position);
        String vc="";
        if(s.getOrderedCount()>=0)
        {
            if(s.getOrderedCount()==0)
                vc="该车位未被下单过";
            else if(s.getOrderedCount()>0)
                vc="该车位预定数量："+s.getOrderedCount()+"。";
        }
        textView.setText(String.format("状态：%s\n%s\n备注：%s",st,vc,total.get(position).getPark().getNote()));
        textView=root.findViewById(R.id.info_price);
        textView.setText(String.format("价格：%.2f",total.get(position).getPark().getPrice()));

        TraitShowController traitShowController=new TraitShowController(root,total.get(position).getTrait());
        Button buy=root.findViewById(R.id.buy);
        boolean buyable= SystemDataMananger.Buyable(total.get(position));
        buy.setTag(total.get(position));
        buy.setEnabled(buyable);
        buy.setBackgroundColor(buyable?fg.getContext().getResources().getColor(R.color.light_blue_400):fg.getContext().getResources().getColor(R.color.banBuy));
        buy.setOnClickListener(new BuyClickListenner());

        return root;
    }
}
